<?php
class Photoalbum extends AppModel
{
	var $name = "Photoalbum";
	var $useTable = false;

    function checkOutdated()
    {
        $lastRunXml = "./files/photoalbum.txt";
        if(file_exists($lastRunXml)) {
            $lastRun = file_get_contents($lastRunXml);
            if(time() - $lastRun >= 86400) {
                //file is outdated
                copy("http://picasaweb.google.com/data/feed/base/user/rcd.foto/", "./files/photoalbum.xml");
                file_put_contents($lastRunXml, time());
			}
		}
		else {
			//xml file does not exist
			copy("http://picasaweb.google.com/data/feed/base/user/rcd.foto", "./files/photoalbum.xml");
			file_put_contents($lastRunXml, time());
		}
    }

    function getNewest($amount)
    {
        //open cached picasa albums
		$file = "./files/photoalbum.xml";
		$parsed_xml =& new XML($file);
		$parsed_xml = Set::reverse($parsed_xml); // this is what i call magic

        $newestAlbums = array();

		for($i=0; $i<$amount; $i++) {
            $newestAlbums[] = $parsed_xml['Feed']['Entry'][$i];
        }

        return $newestAlbums;
    }
	
	function getData()
	{
		if(!isset($_GET['id'])) {
			//open cached picasa albums
			$file = "./files/photoalbum.xml";
			
			$parsed_xml =& new XML($file);
			$parsed_xml = Set::reverse($parsed_xml); // this is what i call magic
			
			//set albums for site
			return $parsed_xml['Feed']['Entry'];
		}
		else {
			$id = $_GET['id'];
			//cache/download picasa photos xml file		
			$lastRunXml = "./files/album{$id}.txt";
			if(file_exists($lastRunXml))
			{
				$lastRun = file_get_contents($lastRunXml);
				if(time() - $lastRun >= 86400)
				{
					copy("http://picasaweb.google.com/data/feed/base/user/rcd.foto/albumid/{$id}?alt=rss&kind=photo&hl=nl", "./files/album{$id}.xml");
					file_put_contents($lastRunXml, time());
				}
			} else {
				copy("http://picasaweb.google.com/data/feed/base/user/rcd.foto/albumid/{$id}?alt=rss&kind=photo&hl=nl", "./files/album{$id}.xml");
				file_put_contents($lastRunXml, time());
			}
			
			//open cached/downloaded file
			$file = "./files/album{$id}.xml";
			
			$parsed_xml =& new XML($file);
			$parsed_xml = Set::reverse($parsed_xml); // this is what i call magic
			
			return $parsed_xml['Rss']['Channel'];
		}
	}
}